﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ComUtil
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Load += new EventHandler(Form1_Load);
        }
        Dictionary<string, CheckBox> checkEditList = null;
        void Form1_Load(object sender, EventArgs e)
        {
            checkEditList = new Dictionary<string, CheckBox>(42);
            this.groupBox1.SuspendLayout();
            int offsetX = 0;
            int offsetY = 0;
            string[] avPort = ComHelper.Instance.GetAvailComPorts();
            for (int i = 0; i < 42; i++)
            {

                CheckBox checkEdit1 = new CheckBox();
                checkEdit1.Enabled = false;
                this.groupBox1.Controls.Add(checkEdit1);
                checkEdit1.Location = new System.Drawing.Point(11 + offsetX * 65 + 2, 21 + offsetY * 19 + 2);

                checkEdit1.Name = "checkEdit1";
                checkEdit1.Text = "COM" + (i + 1).ToString();
                checkEdit1.Size = new System.Drawing.Size(65, 19);
                checkEdit1.TabIndex = 0;
                checkEditList.Add(checkEdit1.Text, checkEdit1);
                offsetX++;
                if (i % 12 == 11)
                {
                    offsetX = 0;
                    offsetY++;
                }

                for (int j = 0; j < avPort.Length; j++)
                {
                    if (avPort[j] == checkEdit1.Text)
                    {
                        checkEdit1.Enabled = true;
                    }
                }
            }


            this.ResumeLayout(true);
        }

        private void btnSeleAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkEditList.Count; i++)
            {
                if (checkEditList.Values.ElementAt<CheckBox>(i).Enabled)
                {
                    checkEditList.Values.ElementAt<CheckBox>(i).Checked = true;
                }
            }
        }

        private void btnUnSeleAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkEditList.Count; i++)
            {
                if (checkEditList.Values.ElementAt<CheckBox>(i).Enabled)
                {
                    checkEditList.Values.ElementAt<CheckBox>(i).Checked = false;
                }
            }
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.listView1.SelectedItems.Count; i++)
            {
                this.textBox1.Text += this.listView1.SelectedItems[0].ToolTipText+"\r\n";
            }
        }

        private void btnExe_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkEditList.Count; i++)
            {
                if (checkEditList.Values.ElementAt<CheckBox>(i).Checked)
                {
                    string comPort = checkEditList.Values.ElementAt<CheckBox>(i).Text;
                    bool result = ComHelper.Instance.RunCmd(comPort, comboBox1.Text, textBox1.Text);
                    if (result)
                    {
                        checkEditList.Values.ElementAt<CheckBox>(i).ForeColor = Color.Green;
                    }
                    else
                    {
                        checkEditList.Values.ElementAt<CheckBox>(i).ForeColor = Color.Red;
                    }

                }
            }
        }
    }
}
